<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>pconvolve</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="pchoct.html" title="pchoct" />
    <link rel="next" href="pcount.html" title="pcount" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">pconvolve</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="pchoct.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="pcount.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="pconvolve"></a>
      <div class="titlepage"></div>
      <a id="IndexPconvolve" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">pconvolve</span>
        </h2>
        <p>convolve — 
      Convolution based on a uniformly partitioned overlap-save algorithm
    </p>
      </div>
      <div class="refsect1">
        <a id="idp133329296"></a>
        <h2>Description</h2>
        <p>
      Convolution based on a uniformly partitioned overlap-save algorithm. Compared to the <a class="link" href="convolve.html" title="convolve"><em class="citetitle">convolve</em></a> opcode, <span class="emphasis"><em>pconvolve</em></span> has these benefits:
    </p>
        <div class="itemizedlist">
          <ul class="itemizedlist" style="list-style-type: disc; ">
            <li class="listitem">
              <p>small delay</p>
            </li>
            <li class="listitem">
              <p>possible to run in real-time for shorter impulse files</p>
            </li>
            <li class="listitem">
              <p>no pre-process analysis pass</p>
            </li>
            <li class="listitem">
              <p>can often render faster than <span class="emphasis"><em>convolve</em></span></p>
            </li>
          </ul>
        </div>
      </div>
      <div class="refsect1">
        <a id="idp133335920"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">ar1 [, ar2] [, ar3] [, ar4] <span class="command"><strong>pconvolve</strong></span> ain, ifilcod [, ipartitionsize, ichannel]</pre>
      </div>
      <div class="refsect1">
        <a id="idp133338176"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>ifilcod</em></span>
      -- integer or character-string denoting an impulse response soundfile. Multichannel files are supported, the file must have the same sample-rate as the orc. [Note: <span class="emphasis"><em>cvanal</em></span> files cannot be used!]  Keep in mind that longer files require more calculation time [and probably larger partition sizes and more latency].  At current processor speeds, files longer than a few seconds may not render in real-time.
    </p>
        <p>
      <span class="emphasis"><em>ipartitionsize</em></span>
      (optional, defaults to the output buffersize [-b]) -- the size in samples of each partition of the impulse file.  This is the parameter that needs tweaking for best performance depending on the impulse file size. Generally, a small size means smaller latency but more computation time. If you specify a value that is not a power-of-2 the opcode will find the next power-of-2 greater and use that as the actual partition size.
    </p>
        <p>
      <span class="emphasis"><em>ichannel</em></span>
      (optional) -- which channel to use from the impulse response data
      file.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp133342800"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>ain</em></span> -- input audio signal.
    </p>
        <p>
      The overall latency of the opcode can be calculated as such [assuming <span class="emphasis"><em>ipartitionsize</em></span> is a power of 2]
    </p>
        <pre class="programlisting">
    ilatency = (ksmps &lt; ipartitionsize ? ipartitionsize + ksmps : ipartitionsize)/sr</pre>
      </div>
      <div class="refsect1">
        <a id="idp133346240"></a>
        <h2>Examples</h2>
        <p>
      Instrument 1 shows an example of real-time convolution.
    </p>
        <p>
    Instrument 2 shows how to do file-based convolution with a 'look ahead' method to remove all delay.
    </p>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note: NOTE">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">NOTE</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
	You can download impulse response files from noisevault.com
	or replace the filenames with your own impulse files.
	</p>
              </td>
            </tr>
          </table>
        </div>
        <p>
      Here is an example of the pconvolve opcode. It uses the file <a class="ulink" href="examples/pconvolve.csd" target="_top"><em class="citetitle">pconvolve.csd</em></a>.
      </p>
        <div class="example">
          <a id="idp133350304"></a>
          <p class="title">
            <strong>Example 611. Example of the pconvolve opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
-odac  <span class="comment">;;;realtime audio out</span>
-iadc    <span class="comment">;;;uncomment -iadc if real audio input is needed too</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o pconvolve.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">ksmps</span> <span class="op">=</span> 32
<span class="ohdr">nchnls</span> <span class="op">=</span> 2
<span class="ohdr">0dbfs</span>  <span class="op">=</span> 1

<span class="oblock">instr</span>   1

kmix <span class="op">=</span> .5	<span class="comment">; Wet/dry mix. Vary as desired.</span>
kvol  <span class="op">=</span> .05<span class="op">*</span>kmix	<span class="comment">; Overall volume level of reverb. May need to adjust</span>
		<span class="comment">; when wet/dry mix is changed, to avoid clipping.</span>
                     			
<span class="comment">; do some safety checking to make sure we the parameters a good</span>
kmix <span class="op">=</span> (kmix <span class="op">&lt;</span> 0 <span class="op">||</span> kmix <span class="op">&gt;</span> 1 <span class="op">?</span> .5 <span class="op">:</span> kmix)
kvol  <span class="op">=</span> (kvol <span class="op">&lt;</span> 0 <span class="op">?</span> 0 <span class="op">:</span> .5<span class="op">*</span>kvol<span class="op">*</span>kmix)

<span class="comment">; size of each convolution partion -- for best performance, this parameter needs to be tweaked</span>
ipartitionsize <span class="op">=</span> p4

<span class="comment">; calculate latency of pconvolve opcode</span>
idel <span class="op">=</span> (<span class="ohdr">ksmps</span> <span class="op">&lt;</span> ipartitionsize <span class="op">?</span> ipartitionsize <span class="op">+</span> <span class="ohdr">ksmps</span> <span class="op">:</span> ipartitionsize)<span class="op">/</span><span class="ohdr">sr</span>
<span class="opc">prints</span> "Convolving with <span class="opc">a</span> latency of <span class="op">%</span>f seconds<span class="op">%</span>n", idel

<span class="comment">; actual processing</span>
al, ar <span class="opc">ins</span>	<span class="comment">;get live input</span>
awetl, awetr <span class="opc">pconvolve</span> kvol<span class="op">*</span>(al<span class="op">+</span>ar), "kickroll.wav", ipartitionsize
<span class="comment">; Delay dry signal, to align it with the convoled sig</span>
adryl <span class="opc">delay</span> (1<span class="op">-</span>kmix)<span class="op">*</span>al, idel
adryr <span class="opc">delay</span> (1<span class="op">-</span>kmix)<span class="op">*</span>al, idel
      <span class="opc">outs</span> adryl<span class="op">+</span>awetl, adryr<span class="op">+</span>awetr

<span class="oblock">endin</span>
    
<span class="oblock">instr</span> 2

imix <span class="op">=</span> 0.5          	<span class="comment">; Wet/dry mix. Vary as desired.</span>
ivol <span class="op">=</span> .05<span class="op">*</span>imix     	<span class="comment">; Overall volume level of reverb. May need to adjust</span>
                	<span class="comment">; when wet/dry mix is changed, to avoid clipping.</span>
ipartitionsize <span class="op">=</span> 1024	<span class="comment">; size of each convolution partion</span>
idel <span class="op">=</span> (<span class="ohdr">ksmps</span> <span class="op">&lt;</span> ipartitionsize <span class="op">?</span> ipartitionsize <span class="op">+</span> <span class="ohdr">ksmps</span> <span class="op">:</span> ipartitionsize)<span class="op">/</span><span class="ohdr">sr</span>   <span class="comment">; latency of pconvolve opcode</span>

kcount	<span class="opc">init</span>	idel<span class="op">*</span><span class="ohdr">kr</span>
<span class="comment">; since we are using a soundin [instead of ins] we can</span>
<span class="comment">; do a kind of "look ahead" by looping during one k-pass</span>
<span class="comment">; without output, creating zero-latency</span>
<span class="olabel">loop</span><span class="op">:</span>
    asig <span class="opc">soundin</span> p4, 0
    awetl, awetr <span class="opc">pconvolve</span> ivol<span class="op">*</span>(asig),"rv_stereo.wav", ipartitionsize
    adry <span class="opc">delay</span>	(1<span class="op">-</span>imix)<span class="op">*</span>asig,idel  <span class="comment">; Delay dry signal, to align it with</span>
    kcount <span class="op">=</span> kcount <span class="op">-</span> 1
 <span class="octrl">if</span> kcount <span class="op">&gt;</span> 0 <span class="octrl">kgoto</span> <span class="olabel">loop</span>
    <span class="opc">outs</span> awetl<span class="op">+</span>adry, awetr<span class="op">+</span>adry
		
<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="stamnt">i</span> 1 0 20 1024		<span class="comment">;play live for 20 seconds</span>

<span class="stamnt">i</span> 2 20 5 "fox.wav"
<span class="stamnt">i</span> 2 25 5 "flute.a<span class="stamnt">i</span>ff"

<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp133354592"></a>
        <h2>See also</h2>
        <p>
      <a class="link" href="convolve.html" title="convolve"><em class="citetitle">convolve</em></a>, <a class="link" href="dconv.html" title="dconv"><em class="citetitle">dconv</em></a>.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp133357600"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Matt Ingalls</td>
          </tr>
          <tr>
            <td>2004</td>
          </tr>
        </table>
        <p>
    </p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="pchoct.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="pcount.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">pchoct </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> pcount</td>
        </tr>
      </table>
    </div>
  </body>
</html>
